<?php 
	jimport('joomla.application.component.controller');
	//require 'components/com_kinh_doanh/tables/kinh_doanh.php';

	
	class kinh_doanhControllerDanh_sach_san_pham extends JController
	{
        

        //goi don hang len kho lon
        function goi_don_hang_len_kho()
        {
            //echo "s";
            $date = date('Y-m-d H:i:s');
            $ro =  & JTable::getInstance('erp_warehouse_bill_io', 'Table');
            
            $model = $this->getModel("sp_trong_kho_lon");
            $data = $model->ds_sp_kho_lon();
            //print_r($data);die;
            $ro->type = 3;
            $ro->id_warehouse_out = $_SESSION['id_kho'];
            $ro->id_branch = $_SESSION['id_a_b'];
            $ro->date_create = $date;
            $ro->status = 0;
            if (!$ro->store()) { //thuc thi them record  neu co loi se hien thi
                              	     JError::raiseError(500, $row->getError() );
                                        }
                                        
            foreach($data as $row)
            {
                if($_POST['id'.$row->id_pro] > 0)
                {
                    echo $_POST['id'.$row->id_pro]."<br/>";
                    $r =  & JTable::getInstance('erp_warehouse_bill_io_extend', 'Table');
                    $r->id_bill=$ro->id;
                    $r->id_product = $row->id_pro;
                    $r->number = $_POST['id'.$row->id_pro];
                    if (!$r->store()) { //thuc thi them record  neu co loi se hien thi
                              	     JError::raiseError(500, $row->getError() );
                                        }
                    
                }

            }
              
        }
        
	    //danh sach san pham trong kho lon
        function sp_trong_kho_lon()
        {
            
            JRequest::setVar('view','sp_trong_kho_lon'); 
			parent::display();  
        }
           
	    //danh sach kho, danh cho nhan vien co id_cha = 0
        function danh_sach_kho()
        {
            JRequest::setVar('view','danh_sach_kho'); 
			parent::display();	 // hien thi    
        } 
         
        //phat sinh luc nhan hang
        function order_phat_sinh()
        {
            $model = $this->getModel("orther_sp");
            $model->nhan_hang_phat_sinh();
            $this->setRedirect("index.php?option=com_kinh_doanh&task=orther_sp","thanh cong"); 
        }
        	   
		//neu khong co task nao phu hop thi mac se chay function nay
		function display()
		{
			JRequest::setVar('view','danh_sach_san_pham'); // goi view dang nhap
			parent::display();	 // hien thi
		}
        
        //nhan hang khi o tren goi xuong
        function nhan_hang()
        {
            
            $model = $this->getModel("orther_sp");
            $model->nhan_hang();
            $this->setRedirect("index.php?option=com_kinh_doanh&task=orther_sp","thanh cong");   
        }
        
        //cap nhat trang thai don hang
        function cap_nhat_orther()
        {
            $model = $this->getModel("orther_sp");
            $data = $model->ds_orther_id_a_b();
            foreach($data as $row)
            {
                if($_POST[$row->id] ==1)
                {
                    $model->xac_nhan_order($row->id);
                }
                if($_POST[$row->id] ==2)
                {
                    $model->chuyen_hang_xuong($row->id);
                    
                }
            }
            $this->setRedirect("index.php?option=com_kinh_doanh&task=orther_sp","thanh cong");   
        }
        
        //xu ly don hang tu khach hang
        function xu_ly_order()
        {
            
            $id_cha = $_POST['id'];
            $date = date('Y-m-d H:i:s'); // ding dang kieu ngay
            //$model = $this->getModel("xu_ly_don_hang");
            $model2 = $this->getModel("dat_sp");
            $data = $model2->ds_sp($id_cha);
            //print_r($data);
            foreach($data as $ro)
            {
                if($_POST[$ro->id_wh] > 0)
                {
                  $row =  & JTable::getInstance('orther', 'Table');
                  $row->id_ab_whouse = $ro->id_wh;//do cac name cua form khac voi cau truc file table nen phai gan tung gia tri
                  $row->id_ab_order = $id_cha;
                  $row->type_i_o = 0;
                  $row->number_of = $_POST[$ro->id_wh];
                  $row->date = $date;
                  $row->status = 0;  
                  if (!$row->store()) { //thuc thi them record  neu co loi se hien thi
                              	     JError::raiseError(500, $row->getError() );
                                        }
                }
            } 
            $this->setRedirect("index.php?option=com_kinh_doanh&task=orther_sp","thanh cong");
        }
        
        
        //dat san pham
        function dat_sp()
        {
                JRequest::setVar('view','dat_sp');
                parent::display();
        }	
            
        
        //nhap them san pham        
        function orther_sp()
        {
            JRequest::setVar('view','orther_sp');
            parent::display();
        } 
        
                //danh sach san pham
                function danh_sach_san_pham()
                {
                    JRequest::setVar('view','danh_sach_san_pham');
                    parent::display();	
                }
                
                //xu ly don hang
                function xu_ly_don_hang()
                {
                    $date = date('Y-m-d H:i:s'); // ding dang kieu ngay
                    $model = $this->getModel("xu_ly_don_hang");
                    $model2 = $this->getModel("thong_tin_don_hang");
			        $data = $model2->lay_id_sp();
                    $id_cus = "";
                    $khach_dua = "";
                    //neu khach thanh toan bang tien mat
                    if($_POST['txtkhachdua']!="")
                    {
                        $khach_dua = $_POST['txtkhachdua'];
                    }
                    if($_POST['txtmkh']!="")
                    {
                        $id_cus = $_POST['txtmkh'];
                        
                        
                    }
                    else
                    {
                        $id_cus = "1";
                       
                    }
                    if($_POST['idcup']!="")
                    {
                        $id_cup = $_POST['idcup'];
                        
                    }
                    else
                    {
                        $id_cup = "";
                    }
                    
                    //echo count($data);die;
                    //xu ly don hang
                    foreach($data as $row)
                    {
                        if(isset($_POST[$row->id_product]))
                        {
                            
                            $obj = json_decode($_POST[$row->id_product]);
                            $id_pro = $obj->id_product;
                            $number = $obj->number;
                            $total = $obj->total;
                            $cur = $obj->id_cur;
                            $model->xu_ly($id_pro,$id_cup,$id_cus,$number,$khach_dua,$total,$cur,$date);
                            
                        }
                    }
                    //neu thanh toan bang the thi cap nhat lai
                    if($_POST['thanhtoan']=="tk_the")
                    {
                        $tt = $_POST['txtsum'];
                        //echo $id_cus;
                        //echo $tt;die;
                        $model->cap_nhat_balance_khach($id_cus,$tt);
                        $this->setRedirect("index.php?option=com_kinh_doanh","Success");
                    }
                    else
                    {
                         $this->setRedirect("index.php?option=com_kinh_doanh","Success");
                    }
                    //print_r($data);
                    //JRequest::setVar('view','xu_ly_don_hang');
                    //parent::display();	
                }
                
                //xu ly phu thu
                function xu_ly_phu_thu()
                {
                    JRequest::setVar('view','xu_ly_phu_thu');
                    parent::display();	
                }
                
                //su luoc ban hang
                function su_luoc_ban_hang()
                {
                    JRequest::setVar('view','su_luoc_ban_hang');
                    parent::display();
                    
                }
                
                //xoa don hang
                function xoa_don_hang()
                {
                    //JRequest::setVar('view','xoa_don_hang');
                    //parent::display();
                    $model = $this->getModel("xoa_don_hang");
                    $id = $_REQUEST['id']; 
                    //echo $gh;die; 
                    $rs=0;
                    $rs = $model->xl_xoa_don_hang($id);	
                    if($rs==1)
                    {
                        $mes= JText::_('thanh cong') ;
                        $this->setRedirect("index.php?option=com_kinh_doanh&task=su_luoc_ban_hang",$mes);
                    }
                    else
                    {
                        $this->setRedirect("index.php?option=com_kinh_doanh&task=su_luoc_ban_hang","that bai");
                    }
                        	
                }
                
                //thong tin don hang
                function thong_tin_don_hang()
                {
                    
                    JRequest::setVar('view','thong_tin_don_hang');
                    parent::display();
                }
        }
?>